package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.cargo.ContractProductVo;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.utils.UUIDUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service //dubbo注解
public class ContractServiceImpl implements ContractService{

    @Autowired
    ContractDao contractDao;

    @Override
    public Contract findById(String id) {
        return contractDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Contract contract) {

        //设置id
        contract.setId(UUIDUtils.getId());

        //初始化总金额,获取数量,附件数量
        contract.setTotalAmount(0d);
        contract.setProNum(0);
        contract.setExtNum(0);

        //设置创建时间
        contract.setCreateTime(new Date());

        //调用dao完成保存操作
        contractDao.insertSelective(contract);
    }

    @Override
    public void update(Contract contract) {

        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void deleteById(String id) {
        //删除此合同相关联的附件记录
        //删除此合同相关联的货物记录
        //删除此合同记录
        contractDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(int page, int size, ContractExample example) {
        PageHelper.startPage(page,size);
        return new PageInfo(contractDao.selectByExample(example));
    }


    @Override
    public List<Contract> findAll(ContractExample example) {
        return contractDao.selectByExample(example);
    }

    @Override
    public List<ContractProductVo> findByShipTime(String shipTime, String companyId) {
        return contractDao.findByShipTime(shipTime,companyId);
    }

    @Override
    public List<User> findName(String module) {
        return contractDao.findName(module);
    }
}
